<template>
  <div class="rightChart">
    <div class="top">
      <div class="title1">实时模式参数配置</div>
    </div>
    <div class="title">捕获设定</div>

    <div class="item">
      <div class="content" v-if="deviceType === 'TOEC01'">
        <div class="label">射频频段：</div>
        <div class="value">
          <el-select
            v-model="realtimeForm.shepinPD"
            style="width: 100%"
            placeholder=""
            @change="changeShepinPD"
            size="mini"
          >
            <el-option
              v-for="item in shepinOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <span style="width: 80px">&emsp;</span>
      </div>
      <div class="content">
        <div class="label">中心频率：</div>
        <div class="value">
          <el-input
            v-model="realtimeForm.center"
            placeholder=""
            size="mini"
            clearable
            @blur="checkCenter1"
            :disabled="isDisable"
          ></el-input>
        </div>
        <span style="width: 80px"
          ><el-select
            v-model="realUnit.center"
            @change="checkCenter1"
            size="mini"
            :disabled="isDisable"
          >
            <el-option label="MHz" value="MHz"></el-option>
            <el-option label="GHz" value="GHz"></el-option>
          </el-select>
        </span>
      </div>
      <div class="content">
        <div class="label">采样率：</div>
        <div class="value">
          <el-input :value="getSample()" placeholder="" size="mini"></el-input>
        </div>
        <span style="width: 80px"
          ><el-select :value="realUnit.sample" size="mini" disabled>
            <el-option label="Hz" value="Hz"></el-option>
            <el-option label="KHz" value="KHz"></el-option>
            <el-option label="MHz" value="MHz"></el-option> </el-select
        ></span>
      </div>
      <div class="content">
        <div class="label">中频带宽：</div>
        <div class="value">
          <!-- <el-input
                v-model="realtimeForm.bandwidth"
                placeholder=""
                size="mini"
                clearable
                @blur="checkBandwidth"
              ></el-input> -->
          <el-select
            v-model="realtimeForm.bandwidth"
            style="width: 100%"
            placeholder=""
            @change="realTimeConfig"
            size="mini"
          >
            <el-option
              v-for="item in deviceType === 'TOEC01'
                ? bandwidthOptions
                : bandwidthOptions1"
              :key="item.label"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>

        <span style="width: 80px">
          &emsp;
          <!-- <el-select
                v-model="realUnit.bandwidth"
                @change="checkBandwidth"
                size="mini"
              >
                <el-option label="KHz" value="KHz"></el-option>
                <el-option label="MHz" value="MHz"></el-option>
              </el-select> -->
        </span>
      </div>
    </div>
    <div class="title">幅度</div>
    <div class="item">
      <!-- <div class="content">
        <div class="label">参考电平：</div>
        <div class="value">
          <el-input
            v-model="maxYVal"
            placeholder=""
            size="mini"
            clearable
            @keyup.native.enter="
              $emit('changeYRange', maxYVal * 1, minYVal * 1)
            "
            @blur="$emit('changeYRange', maxYVal * 1, minYVal * 1)"
          ></el-input>
        </div>
        <span style="width: 80px">
          <el-select value="dBm" size="mini">
            <el-option label="dBm" value="dBm"></el-option>
          </el-select>
        </span>
      </div>
      <div class="content">
        <div class="label">最小电平：</div>
        <div class="value">
          <el-input
            v-model="minYVal"
            placeholder=""
            size="mini"
            clearable
            @keyup.native.enter="
              $emit('changeYRange', maxYVal * 1, minYVal * 1)
            "
            @blur="$emit('changeYRange', maxYVal * 1, minYVal * 1)"
          ></el-input>
        </div>
        <span style="width: 80px">
          <el-select value="dBm" size="mini">
            <el-option label="dBm" value="dBm"></el-option>
          </el-select>
        </span>
      </div> -->
      <div class="content">
        <div class="label">增益：</div>
        <div class="value">
            <el-input-number
            v-model="realtimeForm.zengyi"
            size="mini"
            :min="0"
            :max="30"
            :controls="false"
            style="width: 100%;"
          >
          </el-input-number>
          <!-- <el-input
            v-model="realtimeForm.zengyi"
            placeholder=""
            size="mini"
            clearable
          ></el-input> -->
        </div>
        <span style="width: 80px"
          ><el-select value="db" size="mini">
            <el-option label="db" value="db"></el-option> </el-select
        ></span>
      </div>
    </div>
    <div class="title">分辨率</div>
    <div class="item">
      <div class="content">
        <div class="label">分辨率：</div>
        <div class="value">
          <el-input
            v-model="realtimeForm.rbw"
            placeholder=""
            size="mini"
            clearable
            @blur="checkRbw1"
          ></el-input>
        </div>
        <span style="width: 80px">
          <el-select v-model="realUnit.rbw" @change="checkRbw1" size="mini">
            <el-option label="Hz" value="Hz"></el-option>
            <el-option label="KHz" value="KHz"></el-option>
          </el-select>
        </span>
      </div>
    </div>
    <div class="title">采集</div>
    <div class="item">
      <div class="content">
        <div class="label">平滑次数：</div>
        <div class="value">
          <el-input
            v-model="realtimeForm.smooth"
            placeholder=""
            size="mini"
            clearable
          ></el-input>
        </div>
        <span style="width: 80px">&emsp;</span>
      </div>
    </div>
    <div class="title" v-if="deviceType === 'TOEC01'">触发</div>
    <div class="item" v-if="deviceType === 'TOEC01'">
      <div class="content">
        <div class="label">外触发：</div>
        <div class="value" style="text-align: left">
          <el-switch
            v-model="realtimeForm.outTrigger"
            :active-value="1"
            :inactive-value="0"
            active-text="不触发"
            inactive-text="触发"
            active-color="#409eff"
            inactive-color="#ff4949"
            @change="realTimeConfig"
          >
          </el-switch>
        </div>
        <span style="width: 80px">&emsp;</span>
      </div>
    </div>
    <div class="title">存储设置</div>
    <div class="item">
      <div class="content">
        <div class="label">文件前缀：</div>
        <div class="value" style="text-align: left">
          <el-input
            v-model="saveForm.fileName"
            placeholder=""
            size="mini"
            clearable
          ></el-input>
        </div>
        <span style="width: 80px">&emsp;</span>
      </div>
      <div class="content">
        <div class="label">保存时长：</div>
        <div class="value" style="text-align: left">
          <el-input-number
            style="width: 100%"
            v-model="saveForm.time"
            size="mini"
            :min="1"
            :max="30"
            :controls="false"
          >
          </el-input-number>
        </div>
        <span style="width: 80px">
          <el-select value="s" size="mini">
            <el-option label="s" value="s"></el-option>
          </el-select>
        </span>
      </div>
      <div class="content">
        <div class="label">存储：</div>
        <div class="value" style="text-align: left">
          <el-switch
            v-model="saveForm.start"
            :active-value="1"
            :inactive-value="0"
            active-text="开"
            inactive-text="关"
            active-color="#409eff"
            inactive-color="#ff4949"
          >
          </el-switch>
        </div>
        <span style="width: 80px">&emsp;</span>
      </div>
    </div>
  </div>
</template>

<script>
// 引入请求的接口
import axios from "axios";
export default {
  name: "RealTimeSetting",
  data() {
    return {
      realtimeForm: {
        shepinPD: "", // 射频频段
        center: "", // 中心频点
        bandwidth: "", // 中频带宽
        zengyi: "", // 增益设置
        rbw: "", // 分辨率
        smooth: "", // 平滑次数
        detectType: [], // 采集类型
        outTrigger: 1, // 外触发 0表示触发 1表示不触发
      },
      realUnit: {
        center: "MHz", // 中心频点单位
        bandwidth: "MHz", // 中频带宽单位
        rbw: "Hz", // 分辨率单位
        sample: "Hz", // 采样率单位
      },
      isDisable: false,

      saveForm: {
        start: 0,
        fileName: "",
        time: 1,
      },
      typeOptions: [
        /* {
          label: '实时更新',
          value: 1
        }, */
        {
          label: 'FFT平均',
          value: '2'
        },
        {
          label: 'FFT最大',
          value: '3'
        },
        {
          label: 'FFT最小',
          value: '4'
        }
        /* {
          label: 'FFT最大和最小',
          value: 5
        } */
      ],
      shepinOptions: [
        {
          label: "L波段",
          value: 1,
        },
        {
          label: "70MHz",
          value: 2,
        },
        {
          label: "140MHz",
          value: 3,
        },
      ],
      bandwidthOptions: [
        {
          label: "40MHz",
          value: 40000,
        },
        {
          label: "20MHz",
          value: 20000,
        },
        {
          label: "10MHz",
          value: 10000,
        },
        {
          label: "5MHz",
          value: 5000,
        },
        {
          label: "2.5MHz",
          value: 2500,
        },
        {
          label: "1.25MHz",
          value: 1250,
        },
        {
          label: "625KHz",
          value: 625,
        },
        {
          label: "312.5KHz",
          value: 312.5,
        },
        {
          label: "156.25KHz",
          value: 156.2,
        },
      ],
      bandwidthOptions1: [
        {
          label: "27MHz",
          value: 27000,
        },
        {
          label: "17.8MHz",
          value: 17800,
        },
        {
          label: "8MHz",
          value: 8000,
        },
        {
          label: "3.75MHz",
          value: 3750,
        },
        {
          label: "2MHz",
          value: 2000,
        },
        {
          label: "1MHz",
          value: 1000,
        },
        {
          label: "500KHz",
          value: 500,
        },
        {
          label: "250KHz",
          value: 250,
        },
        {
          label: "140KHz",
          value: 140,
        },
      ],

      maxYVal: -50, // 参考电平 => Y轴最大值
      minYVal: -110, // 最小电平 => Y轴最小值

      deviceType: "", // 设备类型
    };
  },
  inject: ["userMark",'detectType1'],
  methods: {
    getValue(val, type1, type2) {
      if (!val) return null;
      if (type1 === type2) {
        return val;
      } else {
        if (type1 === "KHz") {
          if (type2 === "MHz") {
            return val / 1000;
          } else {
            return val / 1000000;
          }
        } else if (type1 === "MHz") {
          if (type2 === "KHz") {
            return val * 1000;
          } else {
            return val / 1000;
          }
        } else {
          if (type2 === "KHz") {
            return val * 1000000;
          } else {
            return val * 1000;
          }
        }
      }
    },
    // 校验中心频点范围 => 实时模式
    checkCenter1() {
      if (this.deviceType === "TOEC01") {
        if (
          this.realtimeForm.center == "" ||
          this.realtimeForm.shepinPD !== 1
        ) {
          return;
        }
        if (this.realUnit.center === "MHz") {
          if (isNaN(this.realtimeForm.center)) {
            this.realtimeForm.center = 950; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.center = Math.max(
              950,
              Math.min(2150, Number(this.realtimeForm.center))
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        } else {
          if (isNaN(this.realtimeForm.center)) {
            this.realtimeForm.center = 0.95; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.center = Math.max(
              0.95,
              Math.min(2.15, Number(this.realtimeForm.center))
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        }
      } else {
        // BB60C设备
        if (this.realtimeForm.center == "") return;

        if (this.realUnit.center === "MHz") {
          if (isNaN(this.realtimeForm.center)) {
            this.realtimeForm.center = 0.1; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.center = Math.max(
              0.1,
              Math.min(6000, Number(this.realtimeForm.center))
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        } else {
          if (isNaN(this.realtimeForm.center)) {
            this.realtimeForm.center = 0.0001; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.center = Math.max(
              0.0001,
              Math.min(6, Number(this.realtimeForm.center))
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        }
      }
    },
    // 校验中频带宽范围 => 实时模式
    checkBandwidth() {
      if (this.realtimeForm.bandwidth == "") return;
      if (this.deviceType === "BB60C") {
        if (this.realUnit.bandwidth === "KHz") {
          // KHz
          if (isNaN(this.realtimeForm.bandwidth)) {
            this.realtimeForm.bandwidth = 200; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.bandwidth = Math.max(
              200,
              Math.min(27000, Number(this.realtimeForm.bandwidth))
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        } else {
          // MHz
          if (isNaN(this.realtimeForm.bandwidth)) {
            this.realtimeForm.bandwidth = 0.2; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.bandwidth = Math.max(
              0.2,
              Math.min(27, Number(this.realtimeForm.bandwidth))
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        }
      } else {
        if (this.realUnit.bandwidth === "KHz") {
          // KHz
          if (isNaN(this.realtimeForm.bandwidth)) {
            this.realtimeForm.bandwidth = 100; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.bandwidth = Math.max(
              100,
              Math.min(40000, Number(this.realtimeForm.bandwidth))
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        } else {
          // MHz
          if (isNaN(this.realtimeForm.bandwidth)) {
            this.realtimeForm.bandwidth = 0.1; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.bandwidth = Math.max(
              0.1,
              Math.min(40, Number(this.realtimeForm.bandwidth))
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        }
      }
    },
    // 校验分辨率范围 => 实时模式
    checkRbw1() {
      if (this.realtimeForm.rbw == "") return;

      if (this.deviceType === "BB60C") {
        const val = this.getSample();
        if (!val) return;
        let sample;
        if (this.realUnit.sample === "Hz") {
          sample = val;
        } else if (this.realUnit.sample === "KHz") {
          sample = val * 1000;
        } else {
          sample = val * 1000000;
        }
        if (this.realUnit.rbw === "Hz") {
          // KHz
          if (isNaN(this.realtimeForm.rbw)) {
            this.realtimeForm.rbw = (sample / 16384).toFixed(0) * 1; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.rbw = Math.max(
              (sample / 16384).toFixed(0) * 1,
              Math.min(
                (sample / 1024).toFixed(0) * 1,
                Number(this.realtimeForm.rbw)
              )
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        } else {
          // KHz
          if (isNaN(this.realtimeForm.rbw)) {
            this.realtimeForm.rbw = (sample / 16384 / 1000).toFixed(0) * 1; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.rbw = Math.max(
              (sample / 16384 / 1000).toFixed(0) * 1,
              Math.min(
                (sample / 1024 / 1000).toFixed(0) * 1,
                Number(this.realtimeForm.rbw)
              )
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        }
      } else {
        const val = this.realtimeForm.bandwidth;
        if (this.realUnit.rbw === "Hz") {
          // KHz
          if (isNaN(this.realtimeForm.rbw)) {
            this.realtimeForm.rbw =
              ((1000 * val * 46.6) / 40 / 16384).toFixed(0) * 1; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.rbw = Math.max(
              ((1000 * val * 46.6) / 40 / 16384).toFixed(0) * 1,
              Math.min(
                ((1000 * val * 46.6) / 40 / 1024).toFixed(0) * 1,
                Number(this.realtimeForm.rbw)
              )
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        } else {
          // KHz
          if (isNaN(this.realtimeForm.rbw)) {
            this.realtimeForm.rbw =
              ((1000 * val * 46.6) / 40 / 16384 / 1000).toFixed(0) * 1; // 输入的值非数字时，重置为最小值
          } else {
            this.realtimeForm.rbw = Math.max(
              ((1000 * val * 46.6) / 40 / 16384 / 1000).toFixed(0) * 1,
              Math.min(
                ((1000 * val * 46.6) / 40 / 1024 / 1000).toFixed(0) * 1,
                Number(this.realtimeForm.rbw)
              )
            ); // 在范围内时保持原值，否则取最小值或最大值
          }
        }
      }
    },
    // 射频频段修改
    changeShepinPD(newVal) {
      if (newVal == 2) {
        this.realtimeForm.center = 70;
        this.realUnit.center = "MHz";
        this.isDisable = true;
      } else if (newVal == 3) {
        this.realtimeForm.center = 140;
        this.realUnit.center = "MHz";
        this.isDisable = true;
      } else {
        this.isDisable = false;
      }
    },
    // 配置实时模式参数
    async realTimeConfig(url, flag) {
      try {
        await this.realtimeConfigAPI(
          {
            bandwidth: this.realtimeForm.bandwidth,
            center: this.getValue(
              this.realtimeForm.center,
              this.realUnit.center,
              "KHz"
            ),
            rbw:
              this.realUnit.rbw === "Hz"
                ? this.realtimeForm.rbw
                : this.realtimeForm.rbw * 1000,
            shepinPD:
              this.deviceType === "TOEC01" ? this.realtimeForm.shepinPD : null,
            smooth: this.realtimeForm.smooth,
            zengyi: this.realtimeForm.zengyi * 1,
            detectType: this.detectType1.join(","),
            outTrigger:
              this.deviceType === "TOEC01"
                ? this.realtimeForm.outTrigger
                : null,
            // userName: "1_" + this.userMark,
            userName: "1_123456789",
          },
          url
        );
        if (flag === "noClose" || this.saveForm.fileName == "") return;
        await this.setSaveAPI(this.saveForm, url);
      } catch (error) {
        console.log(error);
        this.$message.error("操作失败！");
      }
    },
    // 获取采样率 => 实时模式
    getSample() {
      if (this.realtimeForm.bandwidth == "") {
        this.realUnit.sample = "Hz";
        return "";
      }
      if (this.deviceType === "BB60C") {
        const val = this.realtimeForm.bandwidth;
        switch (val) {
          case 27000:
            this.realUnit.sample = "MHz";
            return 40;

          case 17800:
            this.realUnit.sample = "MHz";
            return 20;

          case 8000:
            this.realUnit.sample = "MHz";
            return 10;

          case 3750:
            this.realUnit.sample = "MHz";
            return 5;

          case 2000:
            this.realUnit.sample = "MHz";
            return 2.5;

          case 1000:
            this.realUnit.sample = "MHz";
            return 1.25;

          case 500:
            this.realUnit.sample = "KHz";
            return 625;

          case 250:
            this.realUnit.sample = "KHz";
            return 312.5;

          case 140:
            this.realUnit.sample = "KHz";
            return 156.25;

          default:
            this.realUnit.sample = "Hz";
            return "";
        }
        /* const val = this.getValue(
            this.realtimeForm.bandwidth,
            this.realUnit.bandwidth,
            'KHz'
          ) */

        /* if (val <= 4) {
            this.realUnit.sample = 'Hz'
            return 4882.8125
          } else if (val <= 8) {
            this.realUnit.sample = 'Hz'
            return 9765.625
          } else if (val <= 15) {
            this.realUnit.sample = 'Hz'
            return 19531.25
          } else if (val <= 30) {
            this.realUnit.sample = 'Hz'
            return 39062.5
          } else if (val <= 65) {
            this.realUnit.sample = 'KHz'
            return 78.125
          } else if (val <= 140) {
            this.realUnit.sample = 'KHz'
            return 156.25
          } else if (val <= 250) {
            this.realUnit.sample = 'KHz'
            return 312.5
          } else if (val <= 500) {
            this.realUnit.sample = 'KHz'
            return 625
          } else if (val <= 1000) {
            this.realUnit.sample = 'MHz'
            return 1.25
          } else if (val <= 2000) {
            this.realUnit.sample = 'MHz'
            return 2.5
          } else if (val <= 3750) {
            this.realUnit.sample = 'MHz'
            return 5
          } else if (val <= 8000) {
            this.realUnit.sample = 'MHz'
            return 10
          } else if (val <= 17800) {
            this.realUnit.sample = 'MHz'
            return 20
          } else if (val <= 27000) {
            this.realUnit.sample = 'MHz'
            return 40
          } else {
            this.realUnit.sample = 'Hz'
            return ''
          } */
      } else {
        this.realUnit.sample = "MHz";
        const val = this.realtimeForm.bandwidth / 1000;
        return (val * 46.65) / 40;
      }
    },
    // 实时参数配置接口
    realtimeConfigAPI(data, url) {
      return axios({
        url: "http://" + url + "/interface/realtime/config",
        method: "post",
        data,
      });
    },
    // 实时模式保存接口
    setSaveAPI(data, url) {
      return axios({
        url: "http://" + url + "/realtime/save",
        method: "post",
        data,
      });
    },
    // 类型变化了
    changeDetectType (newList) {
      // console.log('新类型', newList)
      if (!newList.includes('2')) {
        this.$emit('showLine', 'line', false)
      } else {
        this.$emit('showLine', 'line', true)
      }
      if (!newList.includes('3')) {
        this.$emit('showLine', 'line2', false)
      } else {
        this.$emit('showLine', 'line2', true)
      }
      if (!newList.includes('4')) {
        this.$emit('showLine', 'line3', false)
      } else {
        this.$emit('showLine', 'line3', true)
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.rightChart {
  flex: 1;
  // height: 98vh;
  border: 1px solid #3c3c3c;
  margin: 10px 5px;
  display: flex;
  flex-direction: column;
  background-color: #d9dfeb;
  box-sizing: border-box;

  .top {
    display: flex;
    margin-bottom: 5px;
    height: 28px;
    line-height: 28px;

    .title1 {
      padding-left: 5px;
      font-size: 16px;
      font-weight: bold;
      color: #3c3c3c;
    }
  }
  .title {
    height: 22px;
    line-height: 22px;
    font-size: 14px;
    color: #fff;
    background-color: #4d6081;

    text-align-last: left;
    padding-left: 20px;
  }
  .item {
    min-height: 50px;
    padding: 7px 10px;

    .content {
      display: flex;
      height: 30px;
      line-height: 30px;
      margin-bottom: 5px;

      /deep/ .el-input-number {
        .el-input__inner {
          text-align: left;
        }
      }

      .label {
        text-align: left;
        width: 100px;
        font-size: 14px;
      }

      .value {
        flex: 1;
      }
    }
  }
}
</style>
